const {ccclass, property} = cc._decorator;

@ccclass
export default class Helloworld extends cc.Component {

    @property(cc.Label)
    label: cc.Label = null;
    @property(cc.Sprite)
    spr: cc.Sprite = null;
    @property(cc.Label)
    websocketLabel: cc.Label = null;
    @property
    text: string = '1111';
    @property(cc.Asset)
    wssCacert: cc.Asset = null;
    private _wsiSendBinary = null;



    onLoad(){
        this._wsiSendBinary = new WebSocket("wss://echo.websocket.org", [], this.wssCacert.nativeUrl);
        this._wsiSendBinary.binaryType = "arraybuffer";
        this._wsiSendBinary.onopen = function(evt) {
            this.websocketLabel.string = "cases/05_scripting/11_network/NetworkCtrl.js.5";
        };

    }
    
  
    start(){
        //接收消息
        this._wsiSendBinary.onmessage = function(evt) {
            var binary = new Uint16Array(evt.data);
            var binaryStr = 'response bin msg: ';

            var str = '';
            for (var i = 0; i < binary.length; i++) {
                if (binary[i] === 0)
                {
                    str += "\'\\0\'";
                }
                else
                {
                    var hexChar = '0x' + binary[i].toString('16').toUpperCase();
                    str += String.fromCharCode(hexChar);
                }
            }

            binaryStr += str;
            this.respLabel.string = binaryStr;
            this.websocketLabel.string = "cases/05_scripting/11_network/NetworkCtrl.js.6";
        };
        
        this._wsiSendBinary.onerror = function(evt) {
            this.websocketLabel.string = "cases/05_scripting/11_network/NetworkCtrl.js.7";
        };

        this._wsiSendBinary.onclose = function(evt) {
            this.websocketLabel.string = "cases/05_scripting/11_network/NetworkCtrl.js.8";
            // After close, it's no longer possible to use it again, 
            // if you want to send another request, you need to create a new websocket instance
            // self._wsiSendBinary = null;
        };
        

        this.scheduleOnce(function () {
            // this.send();
        }, 1);
    }

    send(){        
        if (!this._wsiSendBinary) { return; }
        if (this._wsiSendBinary.readyState === WebSocket.OPEN)
        {
            this.websocketLabel.string = "cases/05_scripting/11_network/NetworkCtrl.js.9";
            var buf = "Hello WebSocket中文,\0 I'm\0 a\0 binary\0 message\0.";
            
            var arrData = new Uint16Array(buf.length);
            for (var i = 0; i < buf.length; i++) {
                arrData[i] = buf.charCodeAt(i);
            }
            
            this._wsiSendBinary.send(arrData.buffer);
        }
        else
        {
            var warningStr = "send binary websocket instance wasn't ready...";
            this.websocketLabel.string = "cases/05_scripting/11_network/NetworkCtrl.js.10" + warningStr;
            this.scheduleOnce(function () {
                this.send();
            }, 1);
        }
    
    
    }

    onDisable(){

    }




}
